package com.huwaiwai.zookeeper.demo03;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class ZkTest {

    public static void main(String[] args) throws InterruptedException {

            ZkClient zkc = new ZkClient("127.0.0.1:2181", 60000, 10000);
        zkc.deleteRecursive("/super");

            // 对父节点添加监听子节点变化。
            zkc.subscribeChildChanges("/super", new IZkChildListener() {
                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "[parentPath: " + parentPath);
                    System.out.println(name + "[currentChilds: " + currentChilds);
                }
            });

            Thread.sleep(3000);

            zkc.createPersistent("/super");
            Thread.sleep(1000);

            zkc.createPersistent("/super" + "/" + "c1", "c1内容");
            Thread.sleep(1000);

            zkc.createPersistent("/super" + "/" + "c2", "c2内容");
            Thread.sleep(1000);

//            zkc.delete("/super/c2");
//            Thread.sleep(1000);

            zkc.deleteRecursive("/super");
            Thread.sleep(Integer.MAX_VALUE);
    }
}
